layui.define(function (exports) {
    var config = {
        version: '312',  // 版本号，模块js和页面加版本号防止缓存
        // base: 'http://192.168.254.32:9300/',
        // base_server: 'http://192.168.254.32:9300/admin/',
        // upload_url: 'http://192.168.254.32:9300/file/admin/newFile/',
        // base: 'http://c0801.xgongchang.cn:9300/',
        // base_server: 'http://c0801.xgongchang.cn:9300/admin/',
        // upload_url: 'http://c0801.xgongchang.cn:9300/file/admin/newFile/',
        // base: "http://10.3.0.237:9300/",
        // base: "http://ceshi.yunduanzhizao.com:9300/",
        // base_server: "http://ceshi.yunduanzhizao.com:9300/admin/",
        // base_server: "http://10.3.0.237:9300/admin/",
        // upload_url: 'http://ceshi.yunduanzhizao.com:9300/file/admin/file/',
        // upload_url: 'http://10.3.0.237/file/admin/file/',
        base: window.location.origin.includes("https") ? window.location.origin+':19300/' : window.location.origin+':9300/',
        base_server: window.location.origin.includes("https") ? window.location.origin+':19300/warehouse/' : window.location.origin+':9300/warehouse/',
        upload_url: window.location.origin.includes("https") ? window.location.origin+':19300/file/admin/newFile/' : window.location.origin+':9300/file/admin/newFile/',
        ttableName: 'wing',  // 存储表名
        pageTabs: false,   // 是否开启多标签
        openTabCtxMenu: true,   // 是否开启Tab右键菜单
        maxTabNum: 20,  // 最多打开多少个tab
        viewPath: 'components', // 视图位置
        viewSuffix: '.html',  // 视图后缀
        defaultTheme: 'theme-blue',  // 默认主题
        reqPutToPost: false,  // req请求put方法变成post
        cacheTab: true,  // 是否记忆Tab

        // 获取缓存的token
        getToken: function () {
            var cacheData = layui.data(config.tableName);
            if (cacheData) {
                return cacheData.token;
            }
        },
        // 清除token
        removeToken: function () {
            layui.data(config.tableName, {
                key: 'token',
                remove: true
            });
        },
        // 缓存token
        putToken: function (token) {
            layui.data(config.tableName, {
                key: 'token',
                value: token
            });
        },
        // 当前登录的用户
        getUser: function () {
            var cacheData = layui.data(config.tableName);
            if (cacheData) {
                return cacheData.login_user;
            }
        },
        // 缓存user
        putUser: function (user) {
            layui.data(config.tableName, {
                key: 'login_user',
                value: user
            });
        },
        // 获取用户菜单按钮
        getMenuButton: function (url) {
            var cacheData = layui.data(config.tableName);
            if (cacheData) {
                return cacheData.user_menu[url];
            }
        },
        // 缓存用户菜单
        putUserMenu: function (userMenu) {
            console.log(userMenu)
            var menuObj = {};
            menuObj = this.getMenuObj(userMenu, menuObj);
            console.log(menuObj);
            layui.data(config.tableName, {
                key: 'user_menu',
                value: menuObj
            });
        },
        getMenuObj: function (userMenu, menuObj){
            let config = this;
            userMenu.forEach(function (item, index) {
                if(item.url != "javascript:;"){
                    menuObj[item.url] = item.button;
                }
                if(item.subMenus && item.subMenus.length > 0){
                    config.getMenuObj(item.subMenus, menuObj)
                }
            });
            return menuObj;
        },
        // 获取用户所有权限
        getUserAuths: function () {
            var auths = [];
            /* var authorities = config.getUser().authorities;
             for (var i = 0; i < authorities.length; i++) {
                auths.push(authorities[i].authority);
            } */
            return auths;
        },
        // ajax请求的header
        getAjaxHeaders: function (requestUrl) {
            var headers = [];
            /* var token = config.getToken();
            if (token) {
                headers.push({
                    name: 'Authorization',
                    value: 'Bearer ' + token.access_token
                });
            } */
            return headers;
        },
        
        // ajax请求结束后的处理，返回false阻止代码执行
        ajaxSuccessBefore: function (res, requestUrl) {
            if (res.code == 502) {
                config.removeToken();
                layer.msg('登录过期', {icon: 2, time: 1500}, function () {
                    location.reload();
                });
                return false;
            } else if (res.code == 403) {
                layer.msg('没有访问权限', {icon: 2});
            } else if (res.code == 404) {
                layer.msg('404访问不存在', {icon: 2});
            }
            return true;
        },
        // 路由不存在处理
        routerNotFound: function (r) {
            // location.replace('#/template/error/error-404');
            layer.alert('路由' + location.hash + '不存在', {
                title: '提示',
                skin: 'layui-layer-admin',
                btn: [],
                offset: '30px',
                anim: 6,
                shadeClose: true
            });
        },
        // 搜索栏收缩展开
        putOn: function(element){
            let e = document.getElementsByClassName(element)[0];
            let s = document.getElementsByClassName('spread-span')[0];
            let t = document.getElementsByClassName("lay-query-content")[0];
            let c = document.getElementsByClassName("query-lt")[0];
            let h = window.getComputedStyle(c).height;
            if(e.className.indexOf( 'is-spread' ) > -1){
                e.classList.remove("is-spread");
                t.classList.remove("query-ht-auto");
                t.style.cssText = `height:43px`;
                s.innerText = "展开";
            } else {
                e.classList.add("is-spread");
                t.classList.add("query-ht-auto");
                t.style.cssText = `height:${h}`;
                s.innerText = "收起";
            }
        },
        // 缓存临时数据
        putTempData: function (key, value) {
            layui.data(config.tableName, {
                key: key,
                value: value
            });
        },
        // 获取缓存临时数据
        getTempData: function (key) {
            var cacheData = layui.data(config.tableName);
            if (cacheData) {
                return cacheData[key];
            }
        },
        isEmptyObject: function (obj){
            for(var key in obj){
                return false;
            };
            return true;
        }
    };

    // 更新组件缓存
    layui.config({
        version: config.version
    });

    exports('config', config);
});
